package cn.sinata.wanglin.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import cn.sinata.wanglin.db.DBHelper;
import cn.sinata.wanglin.db.model.User;

/**
 *
 * Created by liaoxiang on 16/12/24.
 */

public class UserDao {
    public static final String TABLE_NAME = "wl_users";
    public static final String COLUMN_NAME_ID = "userId";
    public static final String COLUMN_NAME_NICK = "nick";
    public static final String COLUMN_NAME_HEAD_URL = "head_url";

    private static DBHelper dbHelper;
    private UserDao(){}
    public static UserDao getInstance(Context context){
        dbHelper = DBHelper.getInstance(context);
        return Singleton.INSTANCE;
    }

    private static final class Singleton {
        private final static UserDao INSTANCE= new UserDao();
    }

    public void saveUser(String userId,String name,String headUrl){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db != null && db.isOpen()) {
            ContentValues values = new ContentValues();
            values.put(COLUMN_NAME_ID, userId);
            if(name != null) {
                values.put(COLUMN_NAME_NICK, name);
            }
            if (headUrl!= null) {
                values.put(COLUMN_NAME_HEAD_URL, headUrl);
            }
            long id = getId(db, userId);
            if (id == -1) {
                db.insert(TABLE_NAME, null, values);
            } else {
                if (isNeedUpdate(db, userId, name, headUrl)) {
                    db.update(TABLE_NAME,values," id = ? ",new String[]{id+""});
                }
            }
//            db.replace(TABLE_NAME, null, values);
        }
    }

    private long getId(SQLiteDatabase db,String userId){
        Cursor cursor = db.rawQuery("select id from "+TABLE_NAME+" where "+ COLUMN_NAME_ID +" = ? limit 1 ",new String[]{userId});
        if (cursor == null) {
            return -1;
        }
        if (cursor.moveToFirst()) {
            int id =cursor.getInt(cursor.getColumnIndex("id"));
            cursor.close();
            return id;
        } else {
            return -1;
        }
    }

    private boolean isNeedUpdate(SQLiteDatabase db,String userId,String nickName,String headUrl){
        Cursor cursor = db.rawQuery("select * from "+TABLE_NAME+" where "+ COLUMN_NAME_ID +" = ? limit 1 ",new String[]{userId});
        if (cursor == null) {
            return true;
        }
        if (cursor.moveToFirst()) {
            String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_NICK));
            String url = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_HEAD_URL));
            if (TextUtils.equals(name, nickName) && TextUtils.equals(url,headUrl)) {
                return false;
            }
        } else {
            return true;
        }
        cursor.close();
        return true;
    }

    public User getUser(String userId) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        if (db != null && db.isOpen()) {
            Cursor cursor = db.rawQuery("select * from "+ TABLE_NAME
                    + " where "+COLUMN_NAME_ID +" = ? ",new String[]{userId});
            if (cursor != null && cursor.moveToFirst()) {
                String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_NICK));
                String url = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_HEAD_URL));
                User user = new User();
                user.setHeadUrl(url);
                user.setName(name);
                user.setAccount(userId);
                cursor.close();
                return user;
            }
        }
        return null;
    }
}
